﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using ASBCC.Workflow.K2Manager;

namespace ASBCC.Workflow.Client
{
    public class WorkflowActionPanelBase : System.Web.UI.UserControl, IWorkflowActionPanel
    {

        public bool WorkflowButtonVisible
        {
            get
            {
                return  ViewState["WorkflowButtonVisible"] == null ? true : (bool)ViewState["WorkflowButtonVisible"];
            }
            set
            {
                ViewState["WorkflowButtonVisible"] = value;
            }
        }
        private void RegisterActionAgentButton()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("function __wf_getActionAgentButton() {");
            sb.AppendFormat("    return document.getElememtById('{0}');", GetActionButtonAgent().ClientID);
            sb.AppendLine("}");
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "ActionAgent", sb.ToString(), true);
        }

        protected override void OnLoad(EventArgs e)
        {
            RegisterActionAgentButton();
            base.OnLoad(e);
        }

        protected override void OnInit(EventArgs e)
        {
            _btnActionAgent = new Button();
            _btnActionAgent.ID = "btnActionAgent";
            _btnActionAgent.Click += new EventHandler(btnActionAgent_Click); 
            HtmlGenericControl div = new HtmlGenericControl("div");
            div.Style["display"] = "none";
            div.Controls.Add(_btnActionAgent);
            this.Controls.Add(div);
            foreach (Button actionButton in GetActionButtonList())
            {
                actionButton.UseSubmitBehavior = false;
                actionButton.Click += new EventHandler(actionButton_Click);
            }
            base.OnInit(e);
        }

        void actionButton_Click(object sender, EventArgs e)
        {
            IWorkflowPage workflowPage = GetWorkflowPage();
            IWorkflowController workflowController = GetWorkflowController();
            Button actionButton = (Button)sender;
            string actionType = WorkflowUtility.GetActionType(actionButton);
            bool workflowRelated = WorkflowUtility.IsWorkflowRelated(actionButton);
            if (workflowRelated) workflowController.BuildWorkflowArguments(actionType);
            bool res = workflowPage.ExecuteAction(actionType);
            if (!res) return;
            if (workflowRelated) workflowController.ExecuteAction();
            workflowPage.ExecuteActionDone(actionType);
        }

        void btnActionAgent_Click(object sender, EventArgs e)
        {
            IWorkflowPage workflowPage = GetWorkflowPage();
            Dictionary<string, object> formArguments = workflowPage.GetFormArguments();
            string actionType, auditDestination, comment;
            WorkflowUtility.TryGetDictionaryValue(formArguments, WorkflowKeyConsts.ActionType, out actionType);
            WorkflowUtility.TryGetDictionaryValue(formArguments, WorkflowKeyConsts.AuditDestination, out auditDestination);
            WorkflowUtility.TryGetDictionaryValue(formArguments, WorkflowKeyConsts.Comment, out comment);
            IWorkflowController workflowController = GetWorkflowController();
            Button actionButton = GetActionButton(actionType);
            bool workflowRelated = WorkflowUtility.IsWorkflowRelated(actionButton);
            bool res = workflowPage.ExecuteAction(actionType);
            workflowPage.IsExcuteAction = res;
            if (!res) return;
            if (workflowRelated) GetWorkflowController().ExecuteAction();
            workflowPage.ExecuteActionDone(actionType);
        }

        protected void AddAttributesForActionButton(Button actionButton, string actionType, bool workflowRelated, string text)
        {
            actionButton.Attributes["actionType"] = actionType;
            actionButton.Attributes["causesValidation"] = actionButton.CausesValidation.ToString();
            actionButton.Attributes["validationGroup"] = actionButton.ValidationGroup;
            actionButton.Attributes["workflowRelated"] = workflowRelated.ToString();
            actionButton.Text = text;
        }

        protected IWorkflowPage GetWorkflowPage()
        {
            return (IWorkflowPage)Page;
        }

        protected IWorkflowController GetWorkflowController()
        {
            return GetWorkflowPage().GetWorkflowController();
        }

        #region IWorkflowActionPanel Members

        private Button _btnActionAgent;
        public Button GetActionButtonAgent()
        {
            return _btnActionAgent;
        }

        public Button GetActionButton(string actionType)
        {
            foreach (Button actionButton in this.GetActionButtonList())
            {
                if (actionButton.Attributes["actionType"] == actionType) return actionButton;
            }
            return null;
        }

        public virtual List<Button> GetActionButtonList()
        {
            throw new NotImplementedException("GetWorkflowButtonList is not implemented.");
        }

        #endregion
    }

}
